﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace YArchitech.MEP
{
	public class HYMaterial
	{
		public string Name { get; set; }

		public string Description { get; set; }

		public double Dentity { get; set; }

		public double Roughness { get; set; }

		public int TubeType { get; set; }

		public string Annotate { get; set; }

		public string Segment { get; set; }

		public string Schedule { get; set; }

		public HYPipeSpecifications Specifications { get; set; }

		public void WriteXml(XElement xe)
		{
			try
			{
				xe.Add(new object[]
				{
					new XAttribute("Name", this.Name),
					new XAttribute("Type", this.TubeType),
					new XAttribute("Annotate", this.Annotate),
					new XAttribute("Roughness", this.Roughness),
					new XAttribute("Segment", this.Segment),
					new XAttribute("Schedule", this.Schedule)
				});
				XElement xelement = new XElement("Specifications");
				foreach (HYPipeSpecification hypipeSpecification in this.Specifications)
				{
					XElement xelement2 = new XElement("Specification");
					hypipeSpecification.WriteXml(xelement2);
					xelement.Add(xelement2);
				}
				xe.Add(xelement);
			}
			catch
			{
			}
		}

		public void ReadXml(XElement xe)
		{
			try
			{
				this.Name = xe.Attribute("Name").Value;
				this.TubeType = (int)xe.Attribute("Type");
				this.Annotate = xe.Attribute("Annotate").Value;
				this.Roughness = (double)xe.Attribute("Roughness");
				this.Segment = xe.Attribute("Segment").Value;
				this.Schedule = xe.Attribute("Schedule").Value;
				if (this.Specifications == null)
				{
					this.Specifications = new HYPipeSpecifications();
				}
				this.Specifications.Clear();
				foreach (XElement xe2 in xe.Element("Specifications").Elements())
				{
					HYPipeSpecification hypipeSpecification = new HYPipeSpecification();
					hypipeSpecification.ReadXml(xe2);
					this.Specifications.Add(hypipeSpecification);
				}
			}
			catch
			{
			}
		}

		public static List<HYMaterial> ReadAllMaterials(string dataFile)
		{
			List<HYMaterial> result;
			try
			{
				List<HYMaterial> list = new List<HYMaterial>();
				foreach (XElement xe in XElement.Load(dataFile).Elements("Material"))
				{
					HYMaterial hymaterial = new HYMaterial();
					hymaterial.ReadXml(xe);
					list.Add(hymaterial);
				}
				result = list;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		public static bool SaveAllMaterials(IEnumerable<HYMaterial> matls, string dataFile)
		{
			bool result;
			try
			{
				XElement xelement = new XElement("HYPipeStandard");
				foreach (HYMaterial hymaterial in matls)
				{
					XElement xelement2 = new XElement("Material");
					hymaterial.WriteXml(xelement2);
					xelement.Add(xelement2);
				}
				xelement.Save(dataFile);
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}
	}
}
